const wx2my = require('../../wx2my');

const api = require('../../utils/user_api_miniprogram.js');

const app = getApp();
Page({
  data: {
    marker_msg: false,
    wrap: true,
    noCar: true,
    underway: true,
    have_order: true,
    doorOPenfail: true,
    powerFail: true,
    handFail: true, 
    noChest: true,
    no_order: true,
    openSus: true,
    paySus: true,
    openAgain: 'openAgain',
    handBackagain: 'handBackagain',
    powerBackagain: 'powerBackagain',
    have_coupon: true,
    latitude: '',
    longitude: '',
    macno: '',
    handBackMistake: 'handBackMistake',
    backMistake: true,
    marker: '',
    have_twoOrder: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    let that = this;
    wx2my.getSystemInfo({
      success: function (res) {
        //设置map高度，根据当前设备宽高满屏显示
        that.setData({
          view: {
            Height: res.windowHeight,
            Width: res.windowWidth
          }
        });
      }
    });
  },
  onReady: function () {},
  onShow: function () {
    let that = this;
    api.user_location(function (res) {
      console.log(res);

      if (res.code == 1) {
        that.getLocation_fn();
      } else if (res.code == 0) {
        wx2my.showModal({
          title: '提示',
          content: '请前往设置界面打开权限！',

          success(res) {
            if (res.confirm) {
              console.log('用户点击确定');
              wx2my.openSetting({
                success(data) {
                  console.log(data.authSetting["scope.userLocation"]);

                  if (data.authSetting["scope.userLocation"] == true) {
                    wx2my.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 2000,

                      success() {}

                    });
                  }
                }

              });
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }

        });
      }
    });
    that.get_phone();
  },

  //获取客服电话
  get_phone() {
    let that = this;
    app.wxRequest('customer', {
      'type': 3
    }, function (res) {
      that.setData({
        phone: res.data.data.tel
      });
    });
  },

  getLocation_fn: function () {
    let that = this;
    wx2my.getLocation({
      type: 'gcj02',

      success(res) {
        console.log(res);
        let markers = [];
        markers.push({
          id: -1,
          latitude: res.latitude,
          longitude: res.longitude,
          width: 30,
          height: 30,
          name: 'hello',
          iconPath: "/img/40.png",
          title: "",
          anchor: {
            x: .5,
            y: .5
          }
        });
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: markers
        });
        console.log(res.latitude);
        that.get_markers(); //获取标注点
      },

      fail(e) {}

    });
  },
  get_markers: function () {
    let that = this;
    let markers = that.data.markers;
    console.log(that);
    app.wxRequest('nearby', {
      'lat': that.data.latitude,
      'lng': that.data.longitude
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let markers = that.data.markers;
        let list = res.data.data;
        console.log(list);
        list.forEach((item, index) => {
          // console.log("S")
          markers.push({
            id: index,
            latitude: item.lat,
            longitude: item.lng,
            width: 25,
            height: 25,
            iconPath: "/img/39.png",
            title: "",
            anchor: {
              x: .5,
              y: .5
            }
          });
        });
        that.setData({
          shop: list,
          markers: markers
        });
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  //点击merkers
  markertap(e) {
    console.log(e);

    if (e.markerId < 0) {
      return false;
    } else {
      console.log(this.data.shop[e.detail.markerId]);
      this.setData({
        marker_msg: true,
        using_show: false,
        current_marker: this.data.shop[e.detail.markerId]
      });
    }
  },

  controltap(e) {
    var that = this; //重新定位

    this.getLocation_fn();
  },

  destination(e) {
    //导航
    console.log(e);
    wx2my.openLocation({
      latitude: e.currentTarget.dataset.lat * 1,
      longitude: e.currentTarget.dataset.lng * 1,
      scale: 28,
      name: e.currentTarget.dataset.address,
      address: e.currentTarget.dataset.address
    });
  },

  cancel() {
    this.setData({
      wrap: true
    });
  },

  //联系客服
  tel() {
    let that = this;
    app.wxRequest('customer', {}, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let phone = res.data.data.tel;
        wx2my.showModal({
          title: '温馨提示',
          content: '拨打电话:' + res.data.data.tel,

          success(res) {
            if (res.confirm) {
              wx2my.makePhoneCall({
                phoneNumber: phone
              });
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }

        });
      }
    });
  }

});